// pages/order/orderDetail/orderDetail.js
var api = require('../../../api/api.js');
const pay = require('../../../services/pay.js');
var hms = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: {},
    userInfo: wx.getStorageSync("userInfo"),
    hiddenPay: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var orderId = options.id;
    that.setData({
      orderId: orderId
    });
    that.getOrderDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getOrderDetail: function() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: api.OrderDetail,
      data: {
        order_sn: that.data.orderId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.error_code == 0) {
          var created_at = hms.toFormatTime(res.data.orders.created_at * 1000, 'Y-M-D h:m:s');
          var shipping_at = hms.toFormatTime(res.data.orders.shipping_at * 1000, 'Y-M-D h:m:s');
          that.setData({
            orders: res.data.orders,
            created_at: created_at,
            shipping_at: shipping_at
          })
        }
      }
    })
    setTimeout(function () {
      wx.hideLoading();
    }, 1000);

  },

  toPayTap: function() {
    let that = this;
    that.setData({
      hiddenPay: false
    });
  },

  wxPayTap: function (e) {
    let that = this;
    var orderId = that.data.orders.id;
    wx.showLoading({
      "mask": true,
      title: '支付中...'
    });
    pay.payOrder(parseInt(orderId)).then(res => {
      // wx.redirectTo({
      //   url: '/pages/payResult/payResult?status=1&orderId=' + orderId
      // });
      that.getOrderDetail();
    }).catch(res => {
      wx.showToast({
        title: '支付失败',
        image: '/images/icon/icon_error.png'
      })
    });
    wx.hideLoading();
    that.setData({
      hiddenPay: true
    })
  },

  yuePayTap: function () {
    let that = this;
    var orderId = that.data.orders.id;
    var userMoney = that.data.userInfo.money;
    var total = that.data.orders[index].total;
    if (parseInt(userMoney) < parseInt(total)) {
      wx.showToast({
        title: '余额不足',
        image: '/images/icon/icon_error.png'
      })
    } else {
      wx.showLoading({
        "mask": true,
        title: '支付中...'
      });
      wx.request({
        url: api.PaymentOrder,
        data: {
          order: parseInt(orderId),
          code: "balance.pay"
        },
        header: {
          'Content-Type': 'application/json',
          'X-bocang-Authorization': wx.getStorageSync('token')
        },
        method: "POST",
        success: function (res) {
          if (res.data.error_code == 0) {
            wx.showToast({
              title: "支付成功",
              icon: 'success'
            });
            setTimeout(function () {
              wx.hideLoading();
            }, 1000);
            that.getOrderDetail();
          }
        }
      })
    }
    that.setData({
      hiddenPay: true
    })
  },

  //隐藏付款弹窗
  closePopupTap: function () {
    this.setData({
      hiddenPay: true
    })
  },

  //取消订单
  toCancelTap: function (e) {
    let that = this;
    var orderId = that.data.orders.id;
    wx.showLoading({
      title: '取消中...',
    });
    wx.request({
      url: api.OrderCancel,
      data: {
        order: orderId,
        reason: "1"
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.error_code == 0) {
          wx.showToast({
            title: '取消成功',
            icon: "success"
          })
          that.setData({
            page: 1
          })
          that.getOrderList();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '取消失败',
          image: "/images/error.png"
        })
      }
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 300);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})